//
//  TopListViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TopListViewController.h"
#import "ListDetailsTableViewCell.h"
#import "Song.h"
#import "MusicPlayViewController.h"


@interface TopListViewController ()  <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIImageView *bgImageView;
@property (nonatomic, retain)NSString *imageStr;

@property (nonatomic, retain)UIButton *backButton;

@property (nonatomic, retain)UILabel *pubDateLabel;
@property (nonatomic, retain)NSString *pubDate;


@property (nonatomic, retain)UILabel *numberLabel;
@property (nonatomic, retain)NSString *numberStr;

@property (nonatomic, retain)UITableView *songTableView;
@property (nonatomic, retain)NSMutableArray *songArr;
@property (nonatomic, retain)UIButton *playAllButton;



@property (nonatomic, retain)MBProgressHUD *HUD1;





@end

@implementation TopListViewController

-(void)dealloc
{
    [_playAllButton release];
    [_type release];
    [_HUD1 release];
    [_songArr release];
    [_numberStr release];
    [_pubDate release];
    [_imageStr release];
    [_scrollView release];
    [_bgImageView release];
    [_backButton release];
    [_pubDateLabel release];
    [_numberLabel release];
    [_songTableView release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    PlayView *playView = (PlayView*)[window viewWithTag:10001];
    playView.target = self.navigationController;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.backButton.frame = CGRectMake(0, 20, 50, 44);
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 12, 25, 25)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [self.backButton addSubview:imageView];
    [imageView release];
    
//    [self.backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    
    self.HUD1 = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD1];
    self.HUD1.labelText = @"Music is on the way";
    self.HUD1.dimBackground = YES;
    [self.HUD1 show:YES];
    [self doTask];
    [self.view bringSubviewToFront:self.backButton];
    
}

- (void)doTask
{
    NSString *address = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=%@&format=json&offset=0&size=50&from=ios&fields=title,song_id,author,resource_type,havehigh,is_new,has_mv_mobile,album_title,ting_uid,album_id,charge,all_rate&version=5.2.1&from=ios&channel=appstore", self.type];
    self.songArr = [NSMutableArray array];
    [NetHandler getDataWithUrl:address completion:^(id dict) {
       
        NSDictionary *billBoardDic = [dict objectForKey:@"billboard"];
        self.pubDate = [billBoardDic objectForKey:@"update_date"];
        self.pubDate = [NSString stringWithFormat:@"   更新日期: %@", self.pubDate];
        
        NSDictionary *dic11 = [dict objectForKey:@"billboard"];
        self.imageStr = [dic11 objectForKey:@"pic_s640"];
        
        NSArray *arr = [dict objectForKey:@"song_list"];
        for (NSDictionary *dic in arr) {
            Song *song = [[Song alloc] initWithDictionary:dic];
            [self.songArr addObject:song];
            [song release];
        }
        
        self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, VIEW_WIDTH)];
        self.bgImageView.backgroundColor = [UIColor whiteColor];
        [self.bgImageView setImageWithURL:[NSURL URLWithString:[self.imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [self.view addSubview:self.bgImageView];
        [_bgImageView release];
    
        CGFloat aaa = 70.0 / 667;
        
        self.songTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT + 20 - aaa * VIEW_HEIGHT) style:UITableViewStyleGrouped];
 
        self.songTableView.delegate = self;
        self.songTableView.dataSource = self;
        self.songTableView.rowHeight = 60;
        self.songTableView.showsVerticalScrollIndicator = NO;
        self.songTableView.backgroundColor = [UIColor clearColor];
        [self.songTableView registerClass:[ListDetailsTableViewCell class] forCellReuseIdentifier:@"topListReuse"];

        [self.view addSubview:self.songTableView];
        [_songTableView release];
        
        UIView *view11 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 200)];
        
        self.pubDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, VIEW_WIDTH - 100, 30)];
        self.pubDateLabel.text = self.pubDate;
        self.pubDateLabel.textColor = [UIColor whiteColor];
        self.pubDateLabel.font = [UIFont systemFontOfSize:17];
        [view11 addSubview:self.pubDateLabel];
        [_pubDateLabel release];
        
        
        self.playAllButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.playAllButton.frame =CGRectMake(VIEW_WIDTH - 50, 160, 30, 30);
        [self.playAllButton setBackgroundImage:[UIImage imageNamed:@"playAll.png"] forState:UIControlStateNormal];
        [self.playAllButton addTarget:self action:@selector(playAllButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [view11 addSubview:self.playAllButton];
        
        self.songTableView.tableHeaderView = view11;

        [view11 release];
        [self.view bringSubviewToFront:self.backButton];
        [self.HUD1 hide:YES];
    
    }];
    
    
}

- (void)playAllButtonAction:(UIButton *)button
{
    MusicPlayViewController *musicVC = [MusicPlayViewController shareInstance];
    Song *song = [self.songArr firstObject];
    musicVC.playingNumber = 0;
    musicVC.playSongArr = [NSMutableArray array];
    for (Song *song in self.songArr) {
        [musicVC.playSongArr addObject:song.song_id];
    }
    musicVC.playType = NO;
    musicVC.type = @"music";
    musicVC.songID = song.song_id;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 50)];
    lable.text = [NSString stringWithFormat:@"  共%ld首歌", self.songArr.count];
    lable.backgroundColor = [UIColor whiteColor];
    return [lable autorelease];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= -110) {
        scrollView.contentOffset = CGPointMake(0, -110);
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topListReuse" forIndexPath:indexPath];
    cell.song = [self.songArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MusicPlayViewController *musicVC = [MusicPlayViewController shareInstance];
    Song *song = [self.songArr objectAtIndex:indexPath.row];
    musicVC.playingNumber = indexPath.row;
    musicVC.playSongArr = [NSMutableArray array];
    for (Song *song in self.songArr) {
        NSString *songID = song.song_id;
        [musicVC.playSongArr addObject:songID];
    }
    musicVC.type = @"music";
    musicVC.songID = song.song_id;

}



- (void)backButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
